//导入
/**
 * 总路由于地址
 *  集成模块的地址
 * * */
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { createRouter, createWebHistory } from 'vue-router'
import type { RouteOption } from 'vue-router'
import { getToken } from '@/utils/plugins/auth'
import useSettingsStore from '@/store/modules/settings'

import Layout from '@/layout/index.vue'
import system from '@/router/system' //会员管理
import order from '@/router/order' //会员管理
import goods from '@/router/goods' //商品管理
import formDesigner from '@/router/form-designer' //表单生成器

const whiteList = ['/login', '/register'] //登录、注册
// import useUserStore from '@/store/modules/user'
// import { to as tos } from 'await-to-js'
// import { isHttp } from '@/utils/plugins/validate'
// import usePermissionStore from '@/store/modules/permission'

//import { isRelogin } from '@/utils/request'

// 公共路由
export const constantRoutes: RouteOption[] = [
  {
    path: '/redirect',
    component: Layout,
    hidden: false,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index.vue')
      }
    ]
  },
  {
    path: '/',
    component: Layout,
    redirect: '/index',
    hidden: true,
    meta: {
      title: '首页',
      icon: 'House'
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/index.vue'),
        name: 'Index',
        meta: { title: '首页', icon: 'House', affix: true }
      }
    ]
  },
  {
    path: '/cashier-desk',
    hidden: true,
    meta: {
      title: '收银台',
      icon: 'House'
    },
    component: () => import('@/views/cashier-desk/index.vue')
  },
  // {
  //   path: '/',
  //   name: 'index',
  //   redirect: '/system' //重定向url首页地址
  // },
  {
    path: '/login',
    name: 'login',
    hidden: false,
    meta: {
      title: '登录',
      icon: 'House'
    },
    component: () => import('@/views/public/login.vue')
  },
  {
    //系统管理
    path: '/system',
    name: 'system',
    component: Layout,
    children: system,
    hidden: true,
    meta: {
      title: '系统管理',
      icon: ''
    }
  },
  {
    //订单服务
    path: '/order',
    name: 'order',
    component: Layout,
    children: order,
    hidden: true,
    meta: {
      title: '订单',
      icon: 'Document'
    }
  },
  {
    //订单服务
    path: '/goods',
    name: 'goods',
    component: Layout,
    children: goods,
    hidden: true,
    meta: {
      title: '商品',
      icon: 'Goods'
    }
  },
  {
    //表单设计器
    path: '/form-designer',
    name: 'formDesigner',
    component: Layout,
    children: formDesigner,
    hidden: true,
    meta: {
      title: '表单设计器',
      icon: 'Goods'
    }
  },
  {
    path: '/flow-chart',
    name: 'flowChart',
    component: Layout,
    hidden: true,
    fullPath: '/index', //补全路基
    children: [{
      path: 'index', //访问地址/flow-chart/index
      name: 'flowChart', //权限控制的
      hidden: false,
      meta: {
        title: '流程图',
        name: '流程图',
      },
      component: () => import('@/views/flow-chart/index.vue')
    }],
    meta: {
      title: '流程图',
      icon: 'Goods'
    }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'not-found',
    hidden: false,
    component: () => import('@/views/public/404.vue') //404页面
  }
]

// 动态路由，基于用户权限动态去加载
export const dynamicRoutes: RouteOption[] = [
  // {
  //   path: '/system/user-auth',
  //   component: Layout,
  //   hidden: true,
  //   permissions: ['system:user:edit'],
  //   children: [
  //     {
  //       path: 'role/:userId(\\d+)',
  //       component: () => import('@/views/system/user/authRole.vue'),
  //       name: 'AuthRole',
  //       meta: { title: '分配角色', activeMenu: '/system/user', icon: '' }
  //     }
  //   ]
  // }
]

//创建路由
const router = createRouter({
  history: createWebHistory(), //createWebHistory、createWebHashHistory
  routes: constantRoutes
  // 刷新时，滚动条位置还原
  // scrollBehavior(to, from, savedPosition) {
  //   if (savedPosition) {
  //     return savedPosition
  //   } else {
  //     return { top: 0 }
  //   }
  // }
})

// TODO 动态路由，基于用户权限动态去加载

//处理登录信息
router.beforeEach(async (to, from, next) => {
  NProgress.start() // 设置加载进度条(开始..)
  if (getToken()) {
    //是否登录
    to.meta.title && useSettingsStore().setTitle(to.meta.title as string)
    if (to.path === '/login') {
      next({ path: '/' })
    } else {
      next()
    }
  } else {
    // 没有token
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单，直接进入
      next()
    } else {
      next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
      NProgress.done()
    }
  }
})

//加载进度条结束
router.afterEach(() => {
  NProgress.done() // 设置加载进度条(结束..)
})

export default router
